//
//  MVIPCardService.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/7.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MVIPCardService.h"
#import "MVIPCardModel.h"
#import "MyVipCardListModel.h"
#import "MMerchantModel.h"
#import "VIPCardReturnCashModel.h"
#import "VIPCardUseModel.h"
#define GET_VIPCARD_EXCHANGE_DEALLOG_URL [NSString stringWithFormat:@"%@",@"api/Exchange/DealLog"]
#define SET_VIPCARD_PASSWORD_URL [NSString stringWithFormat:@"%@",@"api/Password/Set"]
#define CHANGE_VIPCARD_PASSWORD_URL [NSString stringWithFormat:@"%@",@"api/Password/Change"]
#define VALIDATE_VIPCARD_PASSWORD_URL [NSString stringWithFormat:@"%@",@"api/Password/Validate"]
#define GET_VIPCARD_LIST_URL [NSString stringWithFormat:@"%@",@"api/VIPCard"]
#define GET_SALEMAN_INFORMATION_URL [NSString stringWithFormat:@"%@",@"api/SaleMan"]
#define GET_MERCHANT_INFO_URL  [NSString stringWithFormat:@"%@",@"api/SaleMan"]
#define Get_VIPCARD_COMSUME_LIST_URL [NSString stringWithFormat:@"%@", @"api/VIPCard/Log"]

#define GET_VIPCARD_SHARELIST_URL  [NSString stringWithFormat:@"%@",@"api/VIPCard/Share/Log"]
@implementation MVIPCardService

+(id)sharedClient
{
    static dispatch_once_t pred;
    static MVIPCardService *shared_instance=nil;
    dispatch_once(&pred, ^{
        shared_instance=[[self alloc] init];
        shared_instance.client=[self sharedHTTPClient];
    });
    return  shared_instance;
}

- (void)getVipCardExchangeDealLogWithUserID:(NSInteger)userID complention:(GetVipCardExchangeDealLogHandler)success failure:(GetVipCardExchangeDealLogErrorHandler)failure {

    NSMutableDictionary *pramas = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(userID),@"userID", nil];
    
    [self.client getPath:GET_VIPCARD_EXCHANGE_DEALLOG_URL parameters:pramas success:^(AFHTTPRequestOperation *operation,id responseObject){
        id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
        
        MVIPCardHTTPResult *httpResult = [[MVIPCardHTTPResult alloc]init];
        
        
        success([httpResult getVIPCardResultListWithJSON:JSON]);
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error){
    
        
    }];
    
}

- (void)getVIPCardPassWordWithType:(NSString *)type userId:(NSInteger)userID password:(NSString *)password phoneNumber:(NSString *)phoneNumber completion:(GetVipSystemCompletionHandler)success failure:(GetVipSystemErrorHandler)failure {

    NSMutableDictionary *parmas = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(1),@"Type",@(userID),@"UserID",password,@"Password",phoneNumber,@"Phone", nil];
    
    [self.client postPath:SET_VIPCARD_PASSWORD_URL parameters:parmas success:^(AFHTTPRequestOperation *operation, id response){
    
        if (operation.response.statusCode == 200) {
            
            success(@"设置成功");
        } else {
        
            failure(@"设置操作密码失败");
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
    
        failure(@"网络异常");
    }];
}
- (void)getVipCardChangePwdWithType:(NSString *)type userID:(NSInteger)userID oldPassword:(NSString *)oldPassword newPassword:(NSString *)newPassword completion:(ChangeVipPasswordCompletionHandler)success failure:(ChangeVipPasswordErrorHandler)failure {

    NSMutableDictionary *parmas = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(1),@"Type",@(userID),@"UserID",oldPassword,@"OldPassword",newPassword,@"NewPassword", nil];
    
    [self.client postPath:CHANGE_VIPCARD_PASSWORD_URL parameters:parmas success:^(AFHTTPRequestOperation *operation, id responceObject) {
        NSString *string = [NSString string];
        if (operation.response.statusCode == 203) {
            string = @"原密码输入错误";
        } else if (operation.response.statusCode == 200){
        //18633154490  8888
            string = @"成功修改密码";
        }
        success(string);
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        
        failure(@"网络异常");
    }];
}
- (void)validateVipCardWithUserID:(NSInteger)userID password:(NSString *)password comletion:(ValidateVipPasswordComletionHandler)success failure:(VaildateVipPasswordErrorHandler)failure {

    NSString *path = [NSString stringWithFormat:@"%@?userid=%@&password=%@",VALIDATE_VIPCARD_PASSWORD_URL,@(userID),password];
    NSLog(@"path %@",path);
    [self.client postPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
    
        NSInteger state = operation.response.statusCode;
        success([NSString stringWithFormat:@"%ld",state]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
    
        failure(@"您输入的密码错误或没有设置密码");
    }];
}
- (void)getMyVipCardWithUserID:(NSInteger)userID isFriend:(NSString *)isFriend isUse:(NSString *)isUse completion:(MyVipCardCompletionHandler)success failure:(MyVipCardCompletionError)failure {

    NSMutableDictionary *pramas = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(userID),@"UserID",isFriend,@"isFriend",isUse,@"isUse", nil];
    [self.client getPath:GET_VIPCARD_LIST_URL parameters:pramas success:^(AFHTTPRequestOperation *operation, id responseObject){
        id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
        
        if (JSON) {
            success([[[MyVipCardListHttpResult alloc] init]getVIPCardListResultWithArrayJSON:JSON]);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
    
        failure(@"网络异常");
    }];
}
- (void)getSalemanInfoWithUserId:(NSInteger)userID completion:(MerchantInfoComletionHandle)success failure:(MerchantInfoComletionError)failure {

    NSMutableDictionary *pramas = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(userID), @"userID",nil];
    [self.client getPath:GET_SALEMAN_INFORMATION_URL parameters:pramas success:^(AFHTTPRequestOperation *operation, id responseObject){
        id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
        
//        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[MMerchantHttpResult alloc]getMerchantResultWithJSON:JSON]];
        
        
        success((MMerchantModel *)[[MMerchantHttpResult alloc] getMerchantResultWithJSON:JSON]);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
    
        failure(@"网络异常");
    }];
}

- (void)getMerchantInfoWithMerchantID:(NSString *)merchantID latitude:(NSString *)latitude longtitude:(NSString *)longtitude completion:(DetailMerchantInfoCompletionHandle)success failure:(MerchantInfoComletionError)failure {

    NSDictionary *pramas = [[NSDictionary alloc]initWithObjects:@[merchantID,longtitude,latitude] forKeys:@[@"userid",@"lng",@"lat"]];
    
//    NSDictionary *pramas = [[NSDictionary alloc]initWithObjectsAndKeys:@(merchantID),@"userid",latitude,@"lat",longtitude,@"lng", nil];
    [self.client getPath:GET_MERCHANT_INFO_URL parameters:pramas success:^(AFHTTPRequestOperation *operation, id responseObject){
    
        id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
        
        success((MDetailMerchantModel*)[[MDetailMerchantHttpResult alloc]getMerchantResultWithJSON:JSON]);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
    
        failure(@"网络异常");

    }];
}
- (void)getVipCardComsumeListWithCardID:(NSInteger)cardId pageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize userID:(NSInteger)userID sortType:(NSString *)sortType userName:(NSString *)userName completion:(VIPCardComsumeInfoCompletionHandle)success failure:(VIPCardComsumeInfoCompletionError)failure {

    NSString *ID = (userID == 0) ? nil :[@(userID) stringValue];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(cardId),@"CardID",@(pageIndex),@"Index",@(pageSize),@"Size",sortType,@"Sort",userName,@"Username",ID,@"userID", nil];
    [self.client getPath:Get_VIPCARD_COMSUME_LIST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
        id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
        if (!JSON) {
            failure(@"No data");
            return ;
        }
        
        NSArray *data = JSON[@"Pagers"];
        
        success([[VIPCardHttpResult alloc]getVipCardListWithArray:data]);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
    
    }];
}
- (void)getVipCardUseSatuationWithCardID:(NSInteger)cardID completion:(VIPCardUseSatuationCompletionHandle)success failure:(VIPCardUseSatuationCompletionError)failure {

    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(cardID),@"cardID", nil];
    [self.client getPath:GET_VIPCARD_SHARELIST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
    
        id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
        
        success([[VIPCardUseHttpResult alloc]getVIPCardUseResultWithArray:JSON]);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
    
    }];
}
//- (void)getVIpCardDetailWithCardID:(NSInteger)ID completion:(VIPCardDetailCompletionHandel)success failure:(VIPCardDetialCompletionError)failure {
//
//    NSMutableDictionary *parmas = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@(ID),@"ID", nil];
//    [self.client getPath:GET_VIPCARD_LIST_URL parameters:parmas success:^(AFHTTPRequestOperation *operation, id responseObject){
//        id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
//        if (JSON) {
//            success([[MyVipCardListHttpResult alloc]getVIPCardListResultWithArrayJSON:JSON]);
//        }
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
//    
//    }];
//}
@end
